package com.health.controller.backend;

import com.health.common.PageBean;
import com.health.common.PageResult;
import com.health.common.QueryPageBean;
import com.health.common.Result;
import com.health.domain.CheckGroup;
import com.health.domain.User;
import com.health.service.UserService;
import com.health.util.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user/login")
    public Result login(@RequestBody Map<String, String> map) {
        String username = map.get("username");
        String password = map.get("password");
        String newpassword = DigestUtils.md5DigestAsHex(password.getBytes());
        userService.login(username, newpassword);
        User user = userService.login(username, newpassword);

        if (user != null){
            // 创建加密内容
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("username",user.getUsername());
            hashMap.put("password",user.getPassword());

            // 生成token
            String token = JwtUtils.generateJwt(hashMap);
            return new Result(true, "登陆成功", token);
        }else {
            return new Result(false,"登录失败",null);
        }


    }

 /*   @GetMapping("/user/getUsername")
    public Result findByUsername(){

        User user = userService.findByUsername();

        String userUsername = user.getUsername();

        return new Result(true,"获取当前登录用户名称成功",userUsername);
    }*/

    @PostMapping("/checkgroup/findPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        PageBean<CheckGroup> pageBean = userService.findByPage(queryPageBean);
        Long total = pageBean.getTotal();
        List<CheckGroup> rows = pageBean.getRows();

        return new PageResult(total,rows);
    }
}
